﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
    class DGraph
    {
        private readonly List<NodeTuple> _graphContainer = new List<NodeTuple>(); 

        public void AddNode(Node node)
        {
            _graphContainer.Add(new NodeTuple(node.Id, node));
        }

        public void ConnectNode(int sourceNode, Node toNode)
        {
            _graphContainer.Add(new NodeTuple(sourceNode, toNode));
        }

        public void SortTopologically(int nd)
        {
            foreach (var nodeTuple in _graphContainer)
            {
                if (nodeTuple.ToNode().Visited)
                    continue;

                nodeTuple.ToNode().Visited = true;
                var targetNodeId = nodeTuple.ToNode().Id;
                var temp = _graphContainer.Where(tuple => tuple.IdNodeFrom() == targetNodeId).ToList();

                foreach (var tuple in temp)
                {
                    if (tuple.ToNode().Visited)
                        continue;

                    tuple.ToNode().Visited = true;
                    SortTopologically(tuple.IdNodeFrom());
                }
            }
        }
    }
}
